package com.lineage.data.item_etcitem.add;

import com.lineage.data.executor.ItemExecutor;
import com.lineage.server.datatables.RecordTable;
import com.lineage.server.model.Instance.L1ItemInstance;
import com.lineage.server.model.Instance.L1PcInstance;
import com.lineage.server.serverpackets.S_ItemStatus;
import com.lineage.server.serverpackets.S_ServerMessage;
import com.lineage.server.serverpackets.ServerBasePacket;

public class saveweaponD extends ItemExecutor {
  private int _type;
  
  public static ItemExecutor get() {
    return new saveweaponD();
  }
  
  public void execute(int[] data, L1PcInstance pc, L1ItemInstance item) {
    int targObjId = data[0];
    L1ItemInstance tgItem = pc.getInventory().getItem(targObjId);
    if (tgItem == null)
      return; 
    if (tgItem.isEquipped()) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("\\fR你必须先解除物品装备!"));
      return;
    } 
    if (tgItem.getItem().getType2() == 1 && tgItem.getEnchantLevel() >= 15) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("武器已达到装备保护卷的最高保护阶段。"));
      return;
    } 
    if (tgItem.getItem().getType2() == 2 && tgItem.getEnchantLevel() >= 11) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("防具已达到装备保护卷的最高保护阶段。"));
      return;
    } 
    if (tgItem.getItem().getItemId() >= 120477 && tgItem.getItem().getItemId() <= 120479 && 
      tgItem.getEnchantLevel() >= 9) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("防具已达到装备保护卷的最高保护阶段。"));
      return;
    } 
    int safe_enchant = tgItem.getItem().get_safeenchant();
    boolean isErr = false;
    int use_type = tgItem.getItem().getUseType();
    switch (use_type) {
      case 1:
      case 2:
      case 18:
      case 19:
      case 20:
      case 21:
      case 22:
      case 25:
      case 47:
        if (safe_enchant < 0)
          isErr = true; 
        break;
      default:
        isErr = true;
        break;
    } 
    if (tgItem.getBless() >= 128)
      isErr = true; 
    if (tgItem.getItem().getItemId() >= 301 && tgItem.getItem().getItemId() <= 305)
      isErr = true; 
    if (isErr) {
      pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
      return;
    } 
    if (tgItem != null) {
      if (tgItem.getItem().get_safeenchant() <= -1) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage(79));
        return;
      } 
      if (tgItem.getproctect() || tgItem.getproctect1() || tgItem.getproctect2()) {
        pc.sendPackets((ServerBasePacket)new S_ServerMessage("目前该装备已经受到保护中"));
        return;
      } 
      tgItem.setproctect3(true);
      pc.setproctctran(this._type);
      pc.sendPackets((ServerBasePacket)new S_ItemStatus(tgItem));
      pc.getInventory().saveItem(tgItem, 4);
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("初级装备保护卷轴的力量附着的物品中。"));
      pc.sendPackets((ServerBasePacket)new S_ServerMessage("保护中的装备机率" + this._type + "%强化失败"));
      pc.getInventory().removeItem(item, 1L);
      RecordTable.get().recordFailureArmor1(pc.getName(), item.getAllName(), tgItem.getAllName(), item.getId(), 
          "赋予机率低阶防爆", pc.getIp());
    } 
  }
  
  public void set_set(String[] set) {
    try {
      this._type = Integer.parseInt(set[1]);
    } catch (Exception exception) {}
  }
}
